<?php
// $Id$

/**
 * @file
 * TODO
 */

function _portals_get_settings() {
  $args = func_get_args();
  if (isset($args[0])) {
    if ($args[0] == 'add') {
      return drupal_get_form('_portals_full_add_form');
    }
    elseif ($args[0] == 'edit') {
      return drupal_get_form('_portals_full_edit_form', $args[1]);
    }
    elseif ($args[0] == 'remove') {
      $output = "<br />Are you sure you wish to remove the portal '";
      $output .=  $args[1] . "'?" . drupal_get_form('_portals_full_remove_form', $args[1]);
      return $output;
    }
    elseif ($args[0] == 'child') {
      if (isset($args[1])) {
        return drupal_get_form('_portals_full_child_form', $args[1]);
      }
      else {
        drupal_set_message('No child portal was specified.');
        drupal_goto('admin/settings/portals');
      }
    }
  }
  
}

function _portals_full_edit_form($form_state, $node_name){
  $items = _portals_form_load_defaults_from_portal_name($node_name);
  if ($items == -1) {
    drupal_set_message("No portal was found with the name $child_name.");
    drupal_goto('admin/settings/portals');
  }
  else {
    return array_merge(
      _portals_title_form(TRUE, $node_name, TRUE),
      _portals_main_form($items, TRUE),
      _portals_submit_form()
    );
  }
}
function _portals_full_add_form($form_state){
  return array_merge(
    _portals_title_form(),
    _portals_main_form(),
    _portals_submit_form()
  );
}
function _portals_full_child_form($form_state, $child_name) {
  $items = _portals_form_load_defaults_from_portal_name($child_name);
  if ($items == -1) {
    drupal_set_message("No child portal was found with the name $child_name.");
    drupal_goto('admin/settings/portals');
  }
  else {
  return array_merge(
    _portals_title_form(),
    _portals_main_form($items, FALSE),
    _portals_submit_form()
  );
  }
}
function _portals_full_remove_form($form_state, $node_name){
  $form['portal_name'] = array(
    '#type' => 'value',
    '#value' => $node_name,
  );
  $form['Confirm'] = array(
    '#type' => 'submit',
    '#value' => t('Confirm'),
    '#submit' => array("_portals_process_remove_form"),
  );
  $form['Cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => array("_portals_process_remove_form"),
  );
  return $form;
}

function _portals_title_form ($required = TRUE, $default_value = '', $hide = FALSE) {
  if($hide){
    $form['portal_name'] = array(
      '#type' => 'value',
      '#value' => $default_value,
    );
  }
  else
  {
    $form['portal_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Portal Name'),
      '#description' => t("Name of the portal"),
      '#required' => TRUE,
      '#default_value' => $default_value,
    );
  }
  return $form;
}
function _portals_main_form ($items = array(), $show_all = TRUE) {
  if(($show_all == TRUE) || isset($items['block'])){
    $form['block'] = array(
      '#type' => 'fieldset',
      '#title' => 'Blocks',
      '#collapsible' => TRUE,
      '#collapsed' => isset($items['block']) ? FALSE : TRUE,
      '#tree' => TRUE,
    );
    foreach (module_implements('block') as $module_name) {
      if(($show_all == TRUE) || isset($items['block']["$module_name"])){
        $form['block']["$module_name"] = array(
          '#type' => 'fieldset',
          '#title' => "Module $module_name",
          '#collapsible' => TRUE,
          '#collapsed' => isset($items['block']["$module_name"]) ? FALSE : TRUE,
          '#tree' => TRUE,
        );
        $modules_list = module_invoke($module_name, 'block');
        if (is_array($modules_list)) {
          foreach (module_invoke($module_name, 'block') as $name => $block) {
            $i = 1;
            $block_name = $block['info'];
            if(($show_all == TRUE) || isset($items['block']["$module_name"]["$name"])){
              $form['block']["$module_name"]["$name"] = array(
                '#type' => 'checkbox',
                '#title' => "$block_name",
                '#return_value' => 1,
                '#tree' => TRUE,
                '#default_value' => isset($items['block']["$module_name"]["$name"]) ? 
                                    $items['block']["$module_name"]["$name"] : 
                                    0,
              );
            }
          }
        }
      }
    }
  }
  
  if(($show_all == TRUE) || isset($items['page'])){
    $form['page'] = array(
      '#type' => 'fieldset',
      '#title' => 'Pages',
      '#collapsible' => TRUE,
      '#collapsed' => isset($items['page']) ? FALSE : TRUE,
      '#tree' => TRUE,
    );
    
    foreach (views_get_all_views() as $name => $view) {
      if ($view = views_get_view($name)) {
        foreach ($view->display as $view_items) {
          if (isset($view_items->display_options['path'])) {
            $the_name = $view->name;
            $the_description = $view->description;
            $the_path = $view_items->display_options['path'];
            if(($show_all == TRUE) || isset($items['page']["$the_path"])){
              $form['page']["$the_path"] = array(
                '#type' => 'checkbox',
                '#title' => "$the_name",
                '#return_value' => 1,
                '#description' => t("$the_description"),
                '#tree' => TRUE,
                '#default_value' => isset($items['page']["$the_path"]) ? 
                                    $items['page']["$the_path"] : 
                                    0,
              );
            }
          }
        }
      }
    }
  }

  return $form;
}
function _portals_submit_form ($handler = "_portals_process_addeditchild_form") {

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array("$handler"),
  );
  return $form;
}
function _portals_process_addeditchild_form ($form, &$form_state) {
  if (isset($form_state['values']['block'])) {
    foreach ($form_state['values']['block'] as $module_name => $block_set) {
      foreach ($block_set as $block_name => $block_value) {
        if ($block_value == 1) {
          $items_enabled[] = (object) array('type' => 'block', 'args' => array($module_name, $block_name));
        }
      }
    }
  }
  if (isset($form_state['values']['page'])) {
    foreach ($form_state['values']['page'] as $page_url => $page_value) {
      if ($page_value == 1) {
        $items_enabled[] = (object) array('type' => 'page', 'args' => array($page_url));
      }
    }  
  }
  
  $portal_name = $form_state['values']['portal_name'];
  if (isset($items_enabled)) {
    $portal_settings = serialize($items_enabled);
    $the_pid = _portals_node_portal_portalid_from_name($portal_name);
    if ($the_pid != -1) {
      _portals_save_cnf_to_pid($the_pid, $portal_settings);
      drupal_set_message("Portal $portal_name updated.");
    }
    else {
      _portals_create_portal($portal_name, $portal_settings);
      drupal_set_message("Portal $portal_name created.");
    }
  }
  else {
    drupal_set_message("No items were selected, portal not saved.");
  }
  $form_state['redirect'] = 'admin/settings/portals';
}
function _portals_process_remove_form ($form, &$form_state) {
  if($form_state['values']['op'] == 'Confirm'){
    $portal_name = $form_state['values']['portal_name'];
    portals_include('queries');
    $the_pid = _portals_node_portal_portalid_from_name($portal_name);
    if ($the_pid != -1) {
      _portals_delete_portal($the_pid);
      drupal_set_message("Portal $portal_name removed.");
    }
    else {
      drupal_set_message("Portal $portal_name could not be found.");
    }
  }
  $form_state['redirect'] = 'admin/settings/portals';
}

function _portals_form_load_defaults_from_portal_name($portal_name) {
  portals_include('queries');
  $portal_id = _portals_node_portal_portalid_from_name($portal_name);
  $defaults = array();
  if ($portal_id != -1) {
    foreach (_portals_get_cnf_from_pid($portal_id) as $config_item) { 
      $portal_args = $config_item->args;
      if ($config_item->type == 'block') {
        $defaults['block'][$portal_args[0]][$portal_args[1]] = 1;
      }
      elseif ($config_item->type == 'page') {
        $defaults['page'][$portal_args[0]] = 1;
      }
    }
    return $defaults;
  } else
  {
    return -1;
  }
}